using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System.IO;
public class AB_Editor : EditorWindow
{
    /// <summary>
    /// 获取文件夹
    /// </summary>
    static string GetFolder
    {
        get
        {
            #if UNITY_EDITOR
            string path = Application.streamingAssetsPath + "/AB/";
            #elif UNITY_ANDROID
            string path=Application.dataPath+"/test/AssetBundle/";
            #elif UNITY_IOS
            #endif
            //判断是否存在文件夹
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path;
        }
    }
    [MenuItem("Assets/Test/BuildAssetBundles/pack  %m", false, 100)]
    static void BuildAssetBundles()
    {
        BuildPipeline.BuildAssetBundles("Assets/AssetBundles",
            BuildAssetBundleOptions.None,
            BuildTarget.StandaloneLinux64
            );
        //GetWindow<AB_Editor>("AB包").Show();
    }
    [MenuItem("Assets/Test/BuildAssetBundles/Print   %l", false, 111)]
    static void PrintBundles()
    {
        //Object[] obj = Selection.GetFiltered<Object>(SelectionMode.Assets);
        Object[] obj = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
        for (int i = 0; i < obj.Length; i++)
        {
            //获取资源路径
            string assetPath = AssetDatabase.GetAssetPath(obj[i]);
            Debug.Log(string.Format("资源的路径{0}，资源的名称：{1}", assetPath, obj[i].name));
        }
    }
    [MenuItem("Assets/Test/BuildAssetBundles/LoadAsset   %k", false, 122)]
    static void LoadAsset()
    {
        GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Resources/Player.prefab");
        GameObject.Instantiate(obj);
    }

    [MenuItem("Assets/Test/BuildAssetBundles/手动打包", false, 133)]
    static void PackAbUsHandler()
    {
        BuildPipeline.BuildAssetBundles(GetFolder, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows);
        //通过编辑器下边使用的平台
        //BuildPipeline.BuildAssetBundles(GetFolder, BuildAssetBundleOptions.ChunkBasedCompression, EditorUserBuildSettings.activeBuildTarget;


        //刷新资源
        AssetDatabase.Refresh();
    }
    //清空所有资源名称和后缀
    [MenuItem("Assets/Test/BuildAssetBundles/Clear", false, 144)]
    static void ClearFlags()
    {
        Object[] obj = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
        for (int i = 0; i < obj.Length; i++)
        {
            //获取资源路径
            string assetPath = AssetDatabase.GetAssetPath(obj[i]);
            //获取资源导入器
            AssetImporter al = AssetImporter.GetAtPath(assetPath);
            al.assetBundleName = "";
            Debug.Log(string.Format("资源的路径{0}，资源的名称：{1}", assetPath, obj[i].name));
        }
    }



    [MenuItem("Assets/Test/BuildAssetBundles/自动打包", false, 155)]
    static void AutoPackAb()
    {
        Object[] obj = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
        for (int i = 0; i < obj.Length; i++)
        {
            //获取资源路径
            string assetPath = AssetDatabase.GetAssetPath(obj[i]);
            //获取资源导入器
            AssetImporter al = AssetImporter.GetAtPath(assetPath);
            al.assetBundleName = obj[i].name;
            al.assetBundleVariant = "main";
            Debug.Log(string.Format("资源的路径{0}，资源的名称：{1}", assetPath, obj[i].name));
        }
        //打包
        //BuildPipeline.BuildAssetBundles(GetFolder, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows);
        BuildTarget target = EditorUserBuildSettings.activeBuildTarget;
        BuildPipeline.BuildAssetBundles(GetFolder, BuildAssetBundleOptions.ChunkBasedCompression,target );
// #elif UNITY_ANDROID
// BuildPipeline.BuildAssetBundles(GetFolder, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.Android);
// #elif UNITY_IOS
// BuildPipeline.BuildAssetBundles(GetFolder, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.iOS);
// #endif

        //刷新资源
        AssetDatabase.Refresh();
        // Object[] objs = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
        // for (int i = 0; i < objs.Length; i++)
        // {
        //     //获取资源路径
        //     string assetPath = AssetDatabase.GetAssetPath(obj[i]);
        //     //获取资源导入器
        //     AssetImporter al = AssetImporter.GetAtPath(assetPath);
        //     al.assetBundleName = "";
        // }
    }
    //清空所有资源名称和后缀
    [MenuItem("Assets/Test/BuildAssetBundles/Clr", false, 166)]
    static void Cr()
    {
        Object[] obj = Selection.GetFiltered<Object>(SelectionMode.DeepAssets);
        for (int i = 0; i < obj.Length; i++)
        {
            //获取资源路径
            string assetPath = AssetDatabase.GetAssetPath(obj[i]);
            //获取资源导入器
            AssetImporter al = AssetImporter.GetAtPath(assetPath);
            al.assetBundleName = "";
            Debug.Log(string.Format("资源的路径{0}，资源的名称：{1}", assetPath, obj[i].name));
        }
    }


}
